#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <stdlib.h>

#define SERVER_IP "192.168.13.133" // 服务器的IP地址
#define SERVER_PORT 9999       // 服务器的端口号
#define SIZE 1024

//注册协议：{user:username,admin:useradmin}
//登录协议：{loginuser:ussername,loginadmin:useradmin}

using namespace std;

class Client
{
private:
    int sockfd;//套接字描述符
    int connect_ret;//连接返回值
    int epfd;
    int reval;
    int cmd;
    char recvbuf[256];
    char buffer[SIZE];
    int select_ret;//select返回值
    char name[256];
    char password[256];
    //文件描述符集合
    fd_set fd_read;
    struct sockaddr_in serverAddr;//定义结构体
public:
    Client(){}
    ~Client(){}
    void InitClient()
    {
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
        {
            cerr << "创建套接字失败" << endl;
            return;
        }
    }
    void ConnectServer()
    {
        //每个字节都用0填充
        memset(&serverAddr,0, sizeof(serverAddr));
        //使用IPv4地址
        serverAddr.sin_family = AF_INET;
        //具体的IP地址
        serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
        //端口号
        serverAddr.sin_port = htons(SERVER_PORT);//主机字节序转网络字节序
        //连接服务器
        connect_ret = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
        if (connect_ret < 0)
        {
            cerr << "连接服务器失败" << endl;
            return;
        }
    }

    void user()
    {
        while(1)
        {
            //清空集合
            FD_ZERO(&fd_read);
            //添加文件描述符到集合
            FD_SET(0, &fd_read);
            //添加文件描述符到集合
            FD_SET(sockfd, &fd_read);

            //等待文件描述符有数据
            select_ret = select(sockfd + 1, &fd_read, NULL, NULL, NULL);
            if(select_ret < 0)
            {
                perror("select失败\n");
                break;
            }
            if(FD_ISSET(sockfd, &fd_read))
            {
                //接收数据
                recv(sockfd, recvbuf, sizeof(recvbuf), 0);
                cout << recvbuf;
                //清空接收缓冲区
                memset(recvbuf, 0, sizeof(recvbuf));
            }
            //判断标准输入是否有数据
            else if(FD_ISSET(0, &fd_read))
            {
                cin >> cmd;
                //注册及登录
                if(cmd == 1)
                {
                    //发送数据
                    cin >> name;
                    cin >> password;
                    sprintf(buffer, "{user:%s,admin:%s}", name, password);
                    send(sockfd, buffer, strlen(buffer)+1, 0);
                    sprintf(buffer, "{loginuser:%s,loginadmin:%s}", name, password);
                    send(sockfd, buffer, strlen(buffer)+1, 0);
                    //清空接收缓冲区
                    memset(buffer, 0, sizeof(buffer));
                    memset(name, 0, sizeof(name));
                    memset(password, 0, sizeof(password));
                }
                //登录
                else if(cmd == 2)
                {
                    cin >> name;
                    cin >> password;
                    sprintf(buffer, "{loginuser:%s,loginadmin:%s}", name, password);
                    send(sockfd, buffer, strlen(buffer)+1, 0);
                    //清空接收缓冲区
                    memset(buffer, 0, sizeof(buffer));
                    memset(name, 0, sizeof(name));
                    memset(password, 0, sizeof(password));
                }
            }
            
        }
    }
    void CloseClient()
    {
        close(sockfd);
    }
};

int main(void)
{
    Client client;
    client.InitClient();
    client.ConnectServer();
    client.user();
    client.CloseClient();
    return 0;
}